import { defineEventHandler, getQuery, setResponseStatus } from 'h3'
import redis from '../util/redis'

// GET /api/scoreboard?key=xxxx  获取一局数据
export default defineEventHandler(async (event) => {
  const { key } = getQuery(event)
  if (!key) {
    setResponseStatus(event, 400)
    return { code: 1, msg: '缺少key' }
  }
  const data = await redis.get('scoreboard:' + key)
  if (!data) {
    setResponseStatus(event, 404)
    return { code: 1, msg: '未找到数据' }
  }
  // 直接返回对象
  try {
    return JSON.parse(data)
  } catch {
    return { code: 1, msg: '数据格式错误' }
  }
})
